setwd("../../") ## replication directory
(rep.dir <- getwd())

library(dgo)
library(plyr)
library(dplyr)
library(foreign)

so <- "dgirt_output_170608_1936-2014_social_varyingitems_states_sample"
eo <- "dgirt_output_170608_1936-2014_economic_varyingitems_states_sample"

### Read
setwd(paste0(rep.dir, "/intermediate-data/subsampled"))
social.opinion.samples.df <- read.dta(paste0(so, ".dta"))
econ.opinion.samples.df <- read.dta(paste0(eo, ".dta"))
setwd(paste0(rep.dir, "/input-data"))
targets <- readRDS(file = "targets.rds")

## Mutate
group.targets <- targets %>%
  mutate(D_year = as.integer(D_year),
         D_black_x_urban = ifelse(
           D_black == "D_blacknon-black" & D_urban == "D_urban0",
           "D_black_x_urban1", NA),
         D_black_x_urban = ifelse(
           D_black == "D_blacknon-black" & D_urban == "D_urban1",
           "D_black_x_urban2", D_black_x_urban),
         D_black_x_urban = ifelse(
           D_black == "D_blackblack", "D_black_x_urban3", D_black_x_urban),
         D_black_x_urban = factor(as.character(D_black_x_urban)))

st.targets <-
  aggregate(Prop ~ D_abb + D_year + D_black_x_urban, group.targets, sum)
st.targets <- as.data.frame(st.targets)
glimpse(st.targets)

### Poststratify
yrs.to.est <- 1936:2014

social.iters <- 1:max(social.opinion.samples.df$iteration)
social.opinion.samples.subset <- social.opinion.samples.df %>%
  filter(iteration %in% social.iters)

if (!identical(sort(grep("D\\_", names(social.opinion.samples.df), value=TRUE)),
               c("D_abb", "D_year"))) {
  social.opinion.samples.st <- social.iters %>%
    llply(function (i) {
      social.opinion.samples.df %>%
        filter(iteration == i) %>%
        poststratify(target_data=st.targets,
                     strata_names=c("D_abb", "D_year"),
                     aggregated_names="D_black_x_urban",
                     proportion_name="Prop") %>%
        mutate(Iteration = i)
    }) %>%
    bind_rows
  summary(social.opinion.samples.st)
} else {
  social.opinion.samples.st <- social.opinion.samples.subset %>%
    mutate(Iteration = iteration)
}
social.opinion.samples.st <- social.opinion.samples.st %>%
  select(StPO = D_abb,
         Year = D_year,
         MassSocialLib = value,
         Iteration = Iteration) %>%
  mutate(StPO = factor(gsub("D\\_abb(.*)", "\\1", StPO)))

econ.iters <- 1:max(econ.opinion.samples.df$iteration)
econ.opinion.samples.subset <- econ.opinion.samples.df %>%
  filter(iteration %in% econ.iters)

if (!identical(sort(grep("D\\_", names(econ.opinion.samples.df), value=TRUE)),
               c("D_abb", "D_year"))) {
  econ.opinion.samples.st <- econ.iters %>%
    llply(function (i) {
      econ.opinion.samples.df %>%
        filter(iteration == i) %>%
        poststratify(target_data=st.targets,
                     strata_names=c("D_abb", "D_year"),
                     aggregated_names="D_black_x_urban",
                     proportion_name="Prop") %>%
        mutate(Iteration = i)
    }) %>%
    bind_rows
  summary(econ.opinion.samples.st)
} else {
  econ.opinion.samples.st <- econ.opinion.samples.subset %>%
    mutate(Iteration = iteration)
}
econ.opinion.samples.st <- econ.opinion.samples.st %>%
  select(StPO = D_abb,
         Year = D_year,
         MassEconLib = value,
         Iteration = Iteration) %>%
  mutate(StPO = factor(gsub("D\\_abb(.*)", "\\1", StPO)))

### Write
setwd(paste0(rep.dir, "/intermediate-data/subsampled"))
write.dta(social.opinion.samples.st, paste0("samples_", so, "-st_est.dta"))
write.dta(econ.opinion.samples.st, paste0("samples_", eo, "-st_est.dta"))

q()
